********************************************************************************
** 	TITLE: f9_secondary_alternatives.do

**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Table I: Effects on Secondary Outcomes

********************************************************************************
set sortseed 13

use "$output_data/estimate.dta", clear


/*********************************************************************************
Panel A: Alternative Recidivism/Misconduct Measures
**********************************************************************************/	
***************************************************************************
*Row 1. Recharged
***************************************************************************
preserve
	local name_r1 = "Recharged"
	
//stay ignite_0 for Recidivism
	cap drop stay
	gen stay = stay_rec
	cap drop ignite_0
	gen ignite_0 = ignite_0_rec
	
	foreach y in charged_3m{
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		//IGNITE
		local pc1: di %8.3f _b[ignite_0]
		local pcse1: di %8.3f _se[ignite_0]
			
		local p_val1 =  el(R,4,1)
		if `p_val1'<0.01 {
			local star1="***"
		}
		if `p_val1'>=0.01 & `p_val1'<0.05 {
			local star1="**"
		}
		if `p_val1'>=0.05 & `p_val1'<0.1 {
			local star1="*"
		}
		if `p_val1'>=0.1 {
			local star1=""
		}
		
		//STAY
		local pc2: di %8.3f _b[stay]
		local pcse2: di %8.3f _se[stay]

		local p_val2 =  el(R,4,2)
		if `p_val2'<0.01 {
			local star2="***"
		}
		if `p_val2'>=0.01 & `p_val2'<0.05 {
			local star2="**"
		}
		if `p_val2'>=0.05 & `p_val2'<0.1 {
			local star2="*"
		}
		if `p_val2'>=0.1 {
			local star2=""
		}
			
		local b1_r1 = "`pc1'" +"`star1'"
		local b1_se_r1 = subinstr("("+"`pcse1'"+")"," ","",.)
		
		local b2_r1 = "`pc2'" +"`star2'"
		local b2_se_r1 = subinstr("("+"`pcse2'"+")"," ","",.)
		local obs_r1: di %8.0gc e(N)
	}
restore 
***************************************************************************
*Row 2. Reconvicted
***************************************************************************
preserve

//Need to bring in gen_appended in order to calculate outcome estimates 
	//and then merge back into estimate sample. 
	local name_r2 = "Reconvicted"

use "$output_data/gen_appended.dta", clear
		
		bys inmate (booking_date): gen convicted_3m = D_convicted[_n+1]==1 if return_3m==1	
		replace convicted_3m = 0 if convicted_3m != 1 & return_3m == 0	

keep inmate booking_date convicted_3m		

tempfile convicted_3mtemp
save `convicted_3mtemp', replace

restore
preserve

merge 1:1 inmate booking_date using `convicted_3mtemp', keep(1 3) nogen

//stay ignite_0 for Recidivism
	cap drop stay
	gen stay = stay_rec
	cap drop ignite_0
	gen ignite_0 = ignite_0_rec
							
	foreach y in convicted_3m{
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		//IGNITE
		local pc1: di %8.3f _b[ignite_0]
		local pcse1: di %8.3f _se[ignite_0]
			
		local p_val1 =  el(R,4,1)
		if `p_val1'<0.01 {
			local star1="***"
		}
		if `p_val1'>=0.01 & `p_val1'<0.05 {
			local star1="**"
		}
		if `p_val1'>=0.05 & `p_val1'<0.1 {
			local star1="*"
		}
		if `p_val1'>=0.1 {
			local star1=""
		}
		
		//STAY
		local pc2: di %8.3f _b[stay]
		local pcse2: di %8.3f _se[stay]

		local p_val2 =  el(R,4,2)
		if `p_val2'<0.01 {
			local star2="***"
		}
		if `p_val2'>=0.01 & `p_val2'<0.05 {
			local star2="**"
		}
		if `p_val2'>=0.05 & `p_val2'<0.1 {
			local star2="*"
		}
		if `p_val2'>=0.1 {
			local star2=""
		}
			
		local b1_r2 = "`pc1'" +"`star1'"
		local b1_se_r2 = subinstr("("+"`pcse1'"+")"," ","",.)
		
		local b2_r2 = "`pc2'" +"`star2'"
		local b2_se_r2 = subinstr("("+"`pcse2'"+")"," ","",.)
		local obs_r2: di %8.0gc e(N)
	}
restore 


***************************************************************************
*Probation Violation
***************************************************************************
preserve

//The probation code already takes into account both booking datasets, 
	//but need gen_appended data to identify spells post-12/2022. 

use "$output_data/gen_appended.dta", clear
	
	merge 1:1 inmate booking_date using "$output_data/probation_violation.dta", keep(1 3) nogen
	sort inmate booking_date
	foreach j in 3{
		gen return_`j'm_probat = .
		replace return_`j'm_probat = return_3m if return_3m <= 0
		replace return_`j'm_probat = 0 if return_3m == 1
		replace return_`j'm_probat = 1 if timegap<=`j'*30 & probat_vi[_n+1] == 1 & return_3m == 1 & inmate[_n+1]==inmate
	}
	
	gen return_3m_noprob = return_3m
	
	replace return_3m_noprob = 0 if return_3m_probat == 1


keep inmate booking_date return_3m_probat return_3m_noprob		

//Going to use prob_data tempfile to first save the outcome measures and then again 
	//to save the merge to estimate sample. 
tempfile prob_data
save `prob_data', replace

restore
preserve	

merge 1:1 inmate booking_date using `prob_data', keep(1 3) nogen

save `prob_data', replace

***************************************************************************
*Row 3. Probation Violation
***************************************************************************


local name_r3 = "Probation Violation"

 use `prob_data', clear
 
//stay ignite_0 for Recidivism
	cap drop stay
	gen stay = stay_rec
	cap drop ignite_0
	gen ignite_0 = ignite_0_rec
				
	foreach y in return_3m_probat{
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		//IGNITE
		local pc1: di %8.3f _b[ignite_0]
		local pcse1: di %8.3f _se[ignite_0]
			
		local p_val1 =  el(R,4,1)
		if `p_val1'<0.01 {
			local star1="***"
		}
		if `p_val1'>=0.01 & `p_val1'<0.05 {
			local star1="**"
		}
		if `p_val1'>=0.05 & `p_val1'<0.1 {
			local star1="*"
		}
		if `p_val1'>=0.1 {
			local star1=""
		}
		
		//STAY
		local pc2: di %8.3f _b[stay]
		local pcse2: di %8.3f _se[stay]

		local p_val2 =  el(R,4,2)
		if `p_val2'<0.01 {
			local star2="***"
		}
		if `p_val2'>=0.01 & `p_val2'<0.05 {
			local star2="**"
		}
		if `p_val2'>=0.05 & `p_val2'<0.1 {
			local star2="*"
		}
		if `p_val2'>=0.1 {
			local star2=""
		}
			
		local b1_r3 = "`pc1'" +"`star1'"
		local b1_se_r3 = subinstr("("+"`pcse1'"+")"," ","",.)
		
		local b2_r3 = "`pc2'" +"`star2'"
		local b2_se_r3 = subinstr("("+"`pcse2'"+")"," ","",.)
		local obs_r3: di %8.0gc e(N)
	}
***************************************************************************
*Row 4. Recidivism without Probation Violation
***************************************************************************
 local name_r4 = "Recidivism without Probation Violation"
 
 use `prob_data', clear
 
//stay ignite_0 for Recidivism
	cap drop stay
	gen stay = stay_rec
	cap drop ignite_0
	gen ignite_0 = ignite_0_rec
				
	foreach y in return_3m_noprob{
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		//IGNITE
		local pc1: di %8.3f _b[ignite_0]
		local pcse1: di %8.3f _se[ignite_0]
			
		local p_val1 =  el(R,4,1)
		if `p_val1'<0.01 {
			local star1="***"
		}
		if `p_val1'>=0.01 & `p_val1'<0.05 {
			local star1="**"
		}
		if `p_val1'>=0.05 & `p_val1'<0.1 {
			local star1="*"
		}
		if `p_val1'>=0.1 {
			local star1=""
		}
		
		//STAY
		local pc2: di %8.3f _b[stay]
		local pcse2: di %8.3f _se[stay]

		local p_val2 =  el(R,4,2)
		if `p_val2'<0.01 {
			local star2="***"
		}
		if `p_val2'>=0.01 & `p_val2'<0.05 {
			local star2="**"
		}
		if `p_val2'>=0.05 & `p_val2'<0.1 {
			local star2="*"
		}
		if `p_val2'>=0.1 {
			local star2=""
		}
			
		local b1_r4 = "`pc1'" +"`star1'"
		local b1_se_r4 = subinstr("("+"`pcse1'"+")"," ","",.)
		
		local b2_r4 = "`pc2'" +"`star2'"
		local b2_se_r4 = subinstr("("+"`pcse2'"+")"," ","",.)
		local obs_r4: di %8.0gc e(N)
	}
restore
***************************************************************************
*Row 5. Minor Misconduct
***************************************************************************
preserve

	local name_r5 = "Minor Misconduct"
replace num_min_weekly = 0 if num_min_weekly == .

//stay ignite_0 for misconduct
	cap drop stay
	gen stay = stay_misc
	cap drop ignite_0
	gen ignite_0 = ignite_0_misc

	foreach y in num_min_weekly {
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		//IGNITE
		local pc1: di %8.3f _b[ignite_0]
		local pcse1: di %8.3f _se[ignite_0]
			
		local p_val1 =  el(R,4,1)
		if `p_val1'<0.01 {
			local star1="***"
		}
		if `p_val1'>=0.01 & `p_val1'<0.05 {
			local star1="**"
		}
		if `p_val1'>=0.05 & `p_val1'<0.1 {
			local star1="*"
		}
		if `p_val1'>=0.1 {
			local star1=""
		}
		
		//STAY
		local pc2: di %8.3f _b[stay]
		local pcse2: di %8.3f _se[stay]

		local p_val2 =  el(R,4,2)
		if `p_val2'<0.01 {
			local star2="***"
		}
		if `p_val2'>=0.01 & `p_val2'<0.05 {
			local star2="**"
		}
		if `p_val2'>=0.05 & `p_val2'<0.1 {
			local star2="*"
		}
		if `p_val2'>=0.1 {
			local star2=""
		}
			
		local b1_r5 = "`pc1'" +"`star1'"
		local b1_se_r5 = subinstr("("+"`pcse1'"+")"," ","",.)
		
		local b2_r5 = "`pc2'" +"`star2'"
		local b2_se_r5 = subinstr("("+"`pcse2'"+")"," ","",.)
		local obs_r5: di %8.0gc e(N)
	}

restore	
***************************************************************************
*Row 6. More Serious Misconduct
***************************************************************************
preserve

	local name_r6 = "More Serious Misconduct"

	//stay ignite_0 for misconduct
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc

******Defining More Serious Misconducts Penal
gen num_penal = 0 
forval inc = 1/90{
replace num_penal = num_penal + 1  if strpos(incidenttype`inc', "MAJ 38") > 0 | strpos(incidenttype`inc', "MAJ 39") > 0 |  ///
strpos(incidenttype`inc', "MAJ 41") > 0
}

	* MI Penal Code misconduct
		gen num_penal_weekly = num_penal/(30*stay/7) 
		replace num_penal_weekly=0 if num_penal==0
		replace num_penal_weekly=num_penal if stay==0
	
//renaming number of major misconducts per week to simplify naming restrictions for estout.
*rename num_maj_weekly n_maj_weekly
rename num_penal_weekly n_p_weekly	
 
	foreach y in n_p_weekly{
		qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		//IGNITE
		local pc1: di %8.3f _b[ignite_0]
		local pcse1: di %8.3f _se[ignite_0]
			
		local p_val1 =  el(R,4,1)
		if `p_val1'<0.01 {
			local star1="***"
		}
		if `p_val1'>=0.01 & `p_val1'<0.05 {
			local star1="**"
		}
		if `p_val1'>=0.05 & `p_val1'<0.1 {
			local star1="*"
		}
		if `p_val1'>=0.1 {
			local star1=""
		}
		
		//STAY
		local pc2: di %8.3f _b[stay]
		local pcse2: di %8.3f _se[stay]

		local p_val2 =  el(R,4,2)
		if `p_val2'<0.01 {
			local star2="***"
		}
		if `p_val2'>=0.01 & `p_val2'<0.05 {
			local star2="**"
		}
		if `p_val2'>=0.05 & `p_val2'<0.1 {
			local star2="*"
		}
		if `p_val2'>=0.1 {
			local star2=""
		}
			
		local b1_r6 = "`pc1'" +"`star1'"
		local b1_se_r6 = subinstr("("+"`pcse1'"+")"," ","",.)
		
		local b2_r6 = "`pc2'" +"`star2'"
		local b2_se_r6 = subinstr("("+"`pcse2'"+")"," ","",.)
		local obs_r6: di %8.0gc e(N)
	}
restore 

***************************************************************************
*Violent and non-violent recidivism
***************************************************************************

//This will also require gen_appended.dta to use post-12/2022 booking spells.

preserve
//We define violent crimes as crimes against persons and crimes with weapons
	//Code below on how Violent Crimes would be defined. 
/******************************************************************************
			
gen D_person_`j' = strpos(lower(c_current_charge`j'),"assault")|strpos(lower(c_current_charge`j'),"homicide")| ///
	 strpos(lower(c_current_charge`j'),"sex")| strpos(lower(c_current_charge`j'),"robbery")| ///
	 strpos(lower(c_current_charge`j'),"stalk")|strpos(lower(c_current_charge`j'),"kidnap")| ///
	 strpos(lower(c_current_charge`j'),"imprison")| ///
	 ( strpos(lower(c_current_charge`j'),"domestic")&strpos(lower(c_current_charge`j'),"violence"))
						
gen D_weapons_`j' =  (strpos(lower(c_current_charge`j'),"weapon")|strpos(lower(c_current_charge`j'),"firearm"))& ///
	D_person_`j'+D_property_`j'+D_drug_`j'+D_po_`j'+D_dui_`j'==0	 
	 
******************************************************************************/	

use "$output_data/sag_gen_updated.dta", clear

	foreach i in 3{ 
		bys sg_id (booking_date): ///
			gen return_`i'm_viol = (D_person[_n+1] | ///
				D_weapons[_n+1]) /// 
				if return_`i'm==1
			replace return_`i'm_viol =0 if return_`i'm==0
			
			gen return_`i'm_nonviol = !return_`i'm_viol ///
				if return_`i'm==1
			replace return_`i'm_nonviol =0 if return_`i'm==0
			}


keep inmate booking_date return_3m_nonviol return_3m_viol		
//Going to use return_violence tempfile to first save the outcome measures 
	//and then again to save the merge to estimate sample. 
tempfile return_violence
save `return_violence', replace

restore
preserve	

merge 1:1 inmate booking_date using `return_violence', keep(1 3) nogen

//stay ignite_0 for Recidivism
	cap drop stay
	gen stay = stay_rec
	cap drop ignite_0
	gen ignite_0 = ignite_0_rec			

save `return_violence', replace

	
***************************************************************************
*Row 7. Violent Recidivism
***************************************************************************
	
	local name_r7 = "Violent Recidivism"

use `return_violence', clear


	replace return_3m = return_3m_viol if return_3m >= 0
	foreach y in return_3m{
		 ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any ) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
		//IGNITE
		local pc1: di %8.3f _b[ignite_0]
		local pcse1: di %8.3f _se[ignite_0]
			
		local p_val1 =  el(R,4,1)
		if `p_val1'<0.01 {
			local star1="***"
		}
		if `p_val1'>=0.01 & `p_val1'<0.05 {
			local star1="**"
		}
		if `p_val1'>=0.05 & `p_val1'<0.1 {
			local star1="*"
		}
		if `p_val1'>=0.1 {
			local star1=""
		}
		
		//STAY
		local pc2: di %8.3f _b[stay]
		local pcse2: di %8.3f _se[stay]

		local p_val2 =  el(R,4,2)
		if `p_val2'<0.01 {
			local star2="***"
		}
		if `p_val2'>=0.01 & `p_val2'<0.05 {
			local star2="**"
		}
		if `p_val2'>=0.05 & `p_val2'<0.1 {
			local star2="*"
		}
		if `p_val2'>=0.1 {
			local star2=""
		}
			
		local b1_r7 = "`pc1'" +"`star1'"
		local b1_se_r7 = subinstr("("+"`pcse1'"+")"," ","",.)
		
		local b2_r7 = "`pc2'" +"`star2'"
		local b2_se_r7 = subinstr("("+"`pcse2'"+")"," ","",.)
		local obs_r7: di %8.0gc e(N)	
	}


*******************************************************************************
*Row 8. Non-violent Recidivism
********************************************************************************	
	local name_r8 = "Non-violent Recidivism"


use `return_violence', clear
	
	replace return_3m = return_3m_nonviol if return_3m >= 0
	foreach y in return_3m{
		 ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any ) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
		
			
		qui ereturn display
		mat R = r(table)
		
		//IGNITE
		local pc1: di %8.3f _b[ignite_0]
		local pcse1: di %8.3f _se[ignite_0]
			
		local p_val1 =  el(R,4,1)
		if `p_val1'<0.01 {
			local star1="***"
		}
		if `p_val1'>=0.01 & `p_val1'<0.05 {
			local star1="**"
		}
		if `p_val1'>=0.05 & `p_val1'<0.1 {
			local star1="*"
		}
		if `p_val1'>=0.1 {
			local star1=""
		}
		
		//STAY
		local pc2: di %8.3f _b[stay]
		local pcse2: di %8.3f _se[stay]

		local p_val2 =  el(R,4,2)
		if `p_val2'<0.01 {
			local star2="***"
		}
		if `p_val2'>=0.01 & `p_val2'<0.05 {
			local star2="**"
		}
		if `p_val2'>=0.05 & `p_val2'<0.1 {
			local star2="*"
		}
		if `p_val2'>=0.1 {
			local star2=""
		}
			
		local b1_r8 = "`pc1'" +"`star1'"
		local b1_se_r8 = subinstr("("+"`pcse1'"+")"," ","",.)
		
		local b2_r8 = "`pc2'" +"`star2'"
		local b2_se_r8 = subinstr("("+"`pcse2'"+")"," ","",.)
		local obs_r8: di %8.0gc e(N)
	}

	restore
************************************************************************************

***************************************************************************
*Make Table
***************************************************************************

	cap file close fh
	cap erase "$tables/iv_alt_measure.tex"
	file open fh using "$tables/iv_alt_measure.tex", write replace
	file write fh ///
			"\vspace{-.4cm} \\ "_n
			
	forval j=1/8{
		file write fh ///
		"`name_r`j'' & `b1_r`j'' & `b2_r`j'' \\ "_n ///
		" ($ N = `obs_r`j'' $) & `b1_se_r`j'' & `b2_se_r`j'' \\ "_n ///
		"\vspace{-.2cm}\\" _n
	}

	cap file close fh
	macro drop fh
	

/*********************************************************************************
Panel B: Other Outcomes
**********************************************************************************/	
***************************************************************************
*Row 7-13. Secondary Outcomes
***************************************************************************

	use "$output_data/estimate.dta", replace
	
 	est clear
	
	//stay ignite_0 for Recidivism
	cap drop stay
	gen stay = stay_rec
	cap drop ignite_0
	gen ignite_0 = ignite_0_rec
	
	cap gen rehab = release_reason == "Rehab Facilities"
		local remove D_remove_any
		local interact interact_proxy_any
		replace prison = 0 if mi(prison)
		local vli tether bonded prison D_convicted rehab ///
			D_sui_weekly D_health_weekly
	
		local name_r `""Tether" "Bail Posted" "Sentenced to Prison" "Convicted" "Released to Rehab. Centers" "Suicide" "Other Medical""'
	
		forval t = 1(1)7{
	local y: word `t' of `vli'
	local nam: word `t' of `name_r'

preserve

	local name_r`t' = "`nam'"
	
			qui ivreghdfe `y' (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if `y'>=0, ///
			a($design_control) cluster(inmate)
			
		qui ereturn display
		mat R = r(table)
		
			//IGNITE
		local pc1: di %8.3f _b[ignite_0]
		local pcse1: di %8.3f _se[ignite_0]
			
		local p_val1 =  el(R,4,1)
		if `p_val1'<0.01 {
			local star1="***"
		}
		if `p_val1'>=0.01 & `p_val1'<0.05 {
			local star1="**"
		}
		if `p_val1'>=0.05 & `p_val1'<0.1 {
			local star1="*"
		}
		if `p_val1'>=0.1 {
			local star1=""
		}
		
		//STAY
		local pc2: di %8.3f _b[stay]
		local pcse2: di %8.3f _se[stay]

		local p_val2 =  el(R,4,2)
		if `p_val2'<0.01 {
			local star2="***"
		}
		if `p_val2'>=0.01 & `p_val2'<0.05 {
			local star2="**"
		}
		if `p_val2'>=0.05 & `p_val2'<0.1 {
			local star2="*"
		}
		if `p_val2'>=0.1 {
			local star2=""
		}
			
		local b1_r`t' = "`pc1'" +"`star1'"
		local b1_se_r`t' = subinstr("("+"`pcse1'"+")"," ","",.)
		
		local b2_r`t' = "`pc2'" +"`star2'"
		local b2_se_r`t' = subinstr("("+"`pcse2'"+")"," ","",.)
		local obs_r`t': di %8.0gc e(N)

restore	
}


***************************************************************************
*Make Table
***************************************************************************

	cap file close fh
	cap erase "$tables/iv_secondary.tex"
	file open fh using "$tables/iv_secondary.tex", write replace
	file write fh ///
			"\vspace{-.4cm} \\ "_n
			
	forval j=1/7{
		file write fh ///
		"`name_r`j'' & `b1_r`j'' & `b2_r`j'' \\ "_n ///
		" ($ N = `obs_r`j'' $) & `b1_se_r`j'' & `b2_se_r`j'' \\ "_n ///
		"\vspace{-.2cm}\\" _n
	}

	cap file close fh
	macro drop fh
